<?php

declare(strict_types=1);

namespace App\Game\Job;

use App\Fastapp\Model\AccountRecharge;
use App\Marketing\Model\MarketingTask;
use App\Marketing\Model\MarketingTaskLog;
use Carbon\Carbon;
use Hyperf\AsyncQueue\Job;

class TaskJob extends Job
{
    public function __construct(public int $user_id, public string $event, public int $account_type, public int $data_id = 0)
    {
    }

    public function handle(): void
    {
        $this->task();
    }

    public function task(): void
    {
        if ($this->event == 'TaskSevenDaySignEvent') $this->sevenDaySign();
        if ($this->event == 'AccountRechargeEvent') $this->recharge();
    }

    public function resultTask(?MarketingTask $task): void
    {
        if (!$task) return;
        $today = date('Y-m-d');
        $map = ['user_id' => $this->user_id, 'task_id' => $task->id];
        $query = MarketingTaskLog::query()->where($map);
        if ($task->type == 2) {//每日
            $query->where('date', $today);
        }
        if ($task->type == 3) {//每周
            $t = Carbon::now();
            $start_at = $t->startOfWeek()->toDateString();
            $end_at = $t->endOfWeek()->toDateString();
            $query->whereBetween('date', [$start_at, $end_at]);
        }
        if ($task->type == 4) {//每月
            $t = Carbon::now();
            $start_at = $t->startOfMonth()->toDateString();
            $end_at = $t->endOfMonth()->toDateString();
            $query->whereBetween('date', [$start_at, $end_at]);
        }
        if (!$query->exists()) {
            $map['date'] = $today;
            MarketingTaskLog::query()->create($map);
        }
    }

    public function sevenDaySign(): void
    {
        $tasks = MarketingTask::query()->where(['account_type' => $this->account_type, 'status' => 1, 'event' => 'TaskSevenDaySignEvent'])->orderByDesc('sort')->get();
        $logs = MarketingTaskLog::query()->where(['user_id' => $this->user_id])->whereIn('task_id', $tasks->pluck('id')->toArray())->orderBy('id')->get();
        $last = $logs->last();
        if ($last && $last->date == date('Y-m-d')) return;
        $task_id = $logs->pluck('task_id')->toArray();
        foreach ($tasks as $task) {
            if (!in_array($task->id, $task_id)) {
                $this->resultTask($task);
                return;
            }
        }
    }

    public function recharge(): void
    {
        $amount = AccountRecharge::query()->where(['id' => $this->data_id])->value('amount');
        $total = AccountRecharge::query()->where(['user_id' => $this->user_id])->where('id', '<=', $this->data_id)->sum('amount');
        $map = ['account_type' => $this->account_type, 'status' => 1, 'event' => 'AccountRechargeEvent'];
        foreach (MarketingTask::query()->where($map)->orderByDesc('sort')->get() as $task) {
            if ($task->accumulation && bccomp($total, $task->amount, 4) != -1) $this->resultTask($task);
            if (!$task->accumulation && bccomp($amount, $task->amount, 4) != -1) $this->resultTask($task);
        }
    }
}
